package com.xidian.zhiancloud.controller.equipFacility.ohManager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.equipFacility.OhPlanDraftDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.qo.equipFacility.DefendPlanMakeQO;
import com.xidian.zhiancloud.qo.equipFacility.OhPlanDraftQO;
import com.xidian.zhiancloud.service.equipFacility.OhPlanDraftService;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeVO;
import com.xidian.zhiancloud.vo.equipFacility.OhPlanDraftDTOVO;
import com.xidian.zhiancloud.vo.equipFacility.OhPlanDraftVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 设备设施 检修管理 检修计划制定接口
 *
 * @Author: 任启亮
 * @Date: 2020/11/11
 */
@RestController
@RequestMapping("/OhPlanDraft")
public class OhPlanDraftController extends BaseController {
    @Autowired
    private OhPlanDraftService ohPlanDraftService;

    /**
     * 增加检修计划制定记录
     *
     * @param ohPlanDraftDTO 检修计划制定对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(OhPlanDraftDTO ohPlanDraftDTO) {
        ohPlanDraftDTO.getOhPlanDraft().setCompanyId(getSessionUserCompanyId());
        ohPlanDraftDTO.getOhPlanDraft().setCreator(getSessionUserid());
        ohPlanDraftDTO.getOhPlanDraft().setCreateTime(new Date(System.currentTimeMillis()));
        Boolean result=ohPlanDraftService.add(ohPlanDraftDTO);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取检修计划制定记录
     *
     * @param id 检修计划制定的主键
     * @return 检修计划制定的单条数据
     */
    @GetMapping("/getById")
    public ResultDTO<OhPlanDraftVO> getById(@RequestParam("id") long id) {
        return ResultDTO.successOf(ohPlanDraftService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除检修计划制定记录
     *
     * @param ids 对应主键值
     * @return 返回检修计划制定的删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result=ohPlanDraftService.deleteByIds(ids);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改检修计划制定记录
     *
     * @param ohPlanDraftDTO 检修计划制定对象
     * @return 返回检修计划制定修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(OhPlanDraftDTO ohPlanDraftDTO) {
        ohPlanDraftDTO.getOhPlanDraft().setCompanyId(null);
        ohPlanDraftDTO.getOhPlanDraft().setCreator(null);
        ohPlanDraftDTO.getOhPlanDraft().setCreateTime(null);
        ohPlanDraftDTO.getOhPlanDraft().setProgramNo(null);
        Boolean result=ohPlanDraftService.patchById(ohPlanDraftDTO);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     *
     * @param ohPlanDraftQO 传递参数
     * @return 返回分页列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<OhPlanDraftVO>> getListByQO(OhPlanDraftQO ohPlanDraftQO) {
        if (ohPlanDraftQO.getCurrent()== null || ohPlanDraftQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        ohPlanDraftQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(ohPlanDraftService.getListByQO(ohPlanDraftQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 发布检修计划
     * @param id 检修计划id
     * @return 返回是否发布成功
     */
    @PostMapping("/issuePlan")
    public ResultDTO<Boolean> issuePlan(Long id){
        return ResultDTO.successOf(ohPlanDraftService.issuePlan(id),GeneralResponseEnums.SUCCESS);
    }
}
